home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Graphics⁄Sound / RTrace-1.0-src / macmemory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-23  |  7.9 KB  |  213 lines  |  [TEXT/KAHL]

  1. /*****************************************************************************\
  2. * macmemory.c                                                                 *
  3. *                                                                             *
  4. * This file contains code which is specific to the Macintosh.  It contains    *
  5. * procedures which handle memory allocation and deallocation.                 *
  6. \*****************************************************************************/
  7.  
  8. #include <stdlib.h>
  9. #include <GestaltEqu.h>
  10. #include "macerrors.h"
  11.  
  12.  
  13. /* Blocks are arranged in a two-way linked list.  Each block consists of an
  14.     eight-byte header, which points to the previous and next blocks, and
  15.     of the data itself */
  16. typedef
  17. struct block_struct
  18.     {
  19.     struct block_struct    *previous;
  20.     struct block_struct    *next;
  21.     long    data;
  22.     } block;
  23.  
  24. #define    BLOCK_HEADER_SIZE    2*sizeof(block *)
  25.  
  26. /* externals */
  27. extern Boolean scene_in_memory;        /* TRUE if the scene for the current file is in memory */
  28.  
  29. /* globals */
  30. block    *last_block;                    /* pointer to the last block in the list */
  31. block    *first_block;                    /* pointer to the first block in the list */
  32.  
  33. /* Prototypes */
  34. void *mac_alloc(unsigned int size);
  35. void mac_free(void *p);
  36. void free_all(void);
  37. void init_mac_alloc(void);
  38. void update_status_free_memory (void);
  39.  
  40.  
  41. /*****************************************************************************\
  42. * procedure init_mac_alloc                                                    *
  43. *                                                                             *
  44. * Purpose: This procedure initializes the mac-specific memory allocation list *
  45. *                                                                             *
  46. * Created by: Greg Ferrar                                                     *
  47. * Created on: September 1, 1992                                               *
  48. * Modified:                                                                   *
  49. *   WHO          WHEN             WHAT                                        *
  50. *   Greg Ferrar  9/5/92           Changed mem. management to use two-way list *
  51. \*****************************************************************************/
  52.  
  53. void init_mac_alloc(void)
  54. {
  55.  
  56.     short    error;
  57.  
  58.     /* allocate eight bytes for the first block, and eight more for the
  59.         last block */
  60.     first_block = (block *) NewPtr(BLOCK_HEADER_SIZE);
  61.     if (error = MemError())    abortive_error(error);
  62.     last_block = (block *) NewPtr(BLOCK_HEADER_SIZE);
  63.     if (error = MemError())    abortive_error(error);
  64.  
  65.     /* Link the last block to the first block, and vice versa */
  66.     last_block->previous = first_block;
  67.     first_block->next = last_block;
  68.     
  69.     /* Mark these blocks as the ends of the list */
  70.     last_block->next = first_block->previous = NULL;
  71.  
  72. }    /* init_mac_alloc() */
  73.  
  74.  
  75.  
  76. /*****************************************************************************\
  77. * procedure mac_alloc                                                         *
  78. *                                                                             *
  79. * Purpose: This procedure allocates a block of memory.  This block can be     *
  80. *          released either by calling mac_free or by calling free_all.        *
  81. *                                                                             *
  82. * Parameters: size:   size of block                                           *
  83. *             returns pointer to allocation                                   *
  84. *                                                                             *
  85. * Created by: Greg Ferrar                                                     *
  86. * Created on: August 25, 1992                                                 *
  87. * Modified:                                                                   *
  88. *   WHO          WHEN             WHAT                                        *
  89. *   Greg Ferrar  9/1/92           Changed memory management to use list       *
  90. *   Greg Ferrar  9/5/92           Changed mem. management to use two-way list *
  91. \*****************************************************************************/
  92.  
  93. void *mac_alloc(unsigned int size)
  94. {
  95.  
  96.     register block        *p;
  97.  
  98.     /* Allocate the memory */
  99.     p = (block *) malloc (size + BLOCK_HEADER_SIZE);
  100.  
  101.     if (p)    /* allocated */
  102.         {
  103.         
  104.         /* Insert this block between last_block and the one before it */
  105.         p->next = last_block;
  106.         p->previous = last_block->previous;
  107.         last_block->previous = p;
  108.         p->previous->next = p;
  109.         
  110.         }
  111.     
  112.     else    /* didn't allocate */
  113.         {
  114.  
  115.         /* We have to remove the scene from memory, or there won't be enough
  116.             memory to display the error dialog */
  117.         free_all();
  118.         
  119.         /* Update the free memory string */
  120.         update_status_free_memory();
  121.         
  122.         /* Show the error dialog and abort */
  123.         abortive_string_error ("Error: not enough memory to finish processing scene.");
  124.  
  125.         }
  126.     
  127.     /* return a pointer to the data */
  128.     return &(p->data);
  129.  
  130. }    /* mac_alloc() */
  131.  
  132.  
  133.  
  134. /*****************************************************************************\
  135. * procedure mac_free                                                          *
  136. *                                                                             *
  137. * Purpose: This procedure frees a block of memory which was allocated using   *
  138. *          mac_alloc.                                                         *
  139. *                                                                             *
  140. * Created by: Greg Ferrar                                                     *
  141. * Created on: August 25, 1992                                                 *
  142. * Modified:                                                                   *
  143. *   WHO          WHEN             WHAT                                        *
  144. *   Greg Ferrar  9/1/92           Changed memory management to use list       *
  145. *   Greg Ferrar  9/5/92           Changed mem. management to use two-way list *
  146. \*****************************************************************************/
  147.  
  148. void mac_free(void *p)
  149. {
  150.  
  151.     register block    *the_block; 
  152.  
  153.     /* Find the start of the block */
  154.     the_block = (block *) ( ((char *) p) - BLOCK_HEADER_SIZE );
  155.  
  156.     /* Remove the block from the list */
  157.     the_block->previous->next = the_block->next;
  158.     the_block->next->previous = the_block->previous;
  159.     
  160.     /* Free the block */
  161.     free(the_block);
  162.  
  163. }    /* mac_free() */
  164.  
  165.  
  166.  
  167. /*****************************************************************************\
  168. * procedure free_all                                                          *
  169. *                                                                             *
  170. * Purpose: This procedure frees all memory of which was previously allocated  *
  171. *          by mac_alloc.                                                      *
  172. *                                                                             *
  173. * Created by: Greg Ferrar                                                     *
  174. * Created on: August 25, 1992                                                 *
  175. * Modified:                                                                   *
  176. *   WHO          WHEN             WHAT                                        *
  177. *   Greg Ferrar  9/1/92           Changed memory management to use list       *
  178. *   Greg Ferrar  9/5/92           Changed mem. management to use two-way list *
  179. \*****************************************************************************/
  180.  
  181. void free_all(void)
  182. {
  183.  
  184.     block     *current_block;
  185.     block    *previous_block;
  186.  
  187.     /* Start with the block before the last */
  188.     current_block = last_block->previous;
  189.  
  190.     /* Keep freeing the last item in the list until there's nothing left except
  191.         the first block and the last block */
  192.     while (current_block != first_block)
  193.         {
  194.         
  195.         /* Find the link from the current block to the previous one */
  196.         previous_block = current_block->previous;
  197.         
  198.         /* Free the current block, since it's not the last one */
  199.         free(current_block);
  200.         
  201.         /* Go on the the previous block */
  202.         current_block = previous_block;
  203.         
  204.         }
  205.     
  206.     /* Connect the last block to the first block */
  207.     last_block->previous = first_block;
  208.     first_block->next = last_block;
  209.  
  210.     /* The scene is no longer in memory */
  211.     scene_in_memory = TRUE;
  212.  
  213. }    /* free_all() */